package com.nt.thah.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import java.util.Iterator;

/**
 * @Description： 图片处理工具类
 * @Author: ls
 * @Date: 2021/2/3 9:16 上午
 */
public class ImageUtils {

    private final static int QRSIZEE = 300;

    // 二维码颜色
    private static final int BLACK = 0xFF000000;

    // 二维码颜色
    private static final int WHITE = 0xFFFFFFFF;

    /**
     * @Description： 剪切图片
     * @Author: ls
     * @Date: 2021/2/3 10:44 上午
     */
    public static InputStream cutImage(InputStream inputStream, Integer x, Integer y, Integer width, Integer height) throws IOException {
//        String imgPath = "/Users/admin/Desktop/WeChataca510862887cd53703715d7ab4ee2ab.png";
//        File f = new File(imgPath);
        //图片输入流s
        InputStream input = null;
        ImageInputStream iis = ImageIO.createImageInputStream(inputStream);
        //图片读取器
        Iterator<ImageReader> it = ImageIO.getImageReaders(iis);
        if (it.hasNext()) {
            ImageReader r = it.next();
            //设置输入流
            r.setInput(iis, true);
            //读取参数
            ImageReadParam param = r.getDefaultReadParam();
            //创建要截取的矩形范围
//            Rectangle rect = new Rectangle(19, 1166, 154, 154);
            Rectangle rect = new Rectangle(x, y, width, height);
            param.setSourceRegion(rect);
            //读取截图数据
            BufferedImage bi = r.read(0, param);
            // 保存图片
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bi, "png", os);
            input = new ByteArrayInputStream(os.toByteArray());
        }
        return input;
    }

    /**
     * @Description:
     * @Author ls
     * @Date 2021/09/03 10:57
     */
    public static InputStream createImage(String id) throws IOException {
        String content = id;
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRSIZEE, QRSIZEE, hints);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bi.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bi, "png", os);
        return new ByteArrayInputStream(os.toByteArray());
    }

    public static void main(String[] args) throws IOException {
        InputStream image = createImage("https://sme.kcbebank.com/marketing/share.html?redirect_uri=https%3A%2F%2Fsme.kcbebank.com%2Fsme%2F%3FinviterUserNo%3DUR115747711189258241%26channel%3DWECHAT_H5%26timestamp%3DX7EQss8iWdgbAkSloKmSXXKX1aIcbkl7ktBK4PrSMpI%253D%26referenceOpenId%3DoMHYr5oqa533p9vd3Cy9134uMCVU");
    }
}
